package com.chixing.service.impl;

import com.chixing.mapper.CarMapper;
import com.chixing.mapper.MyOrderMapper;
import com.chixing.mapper.StoreCarMapper;
import com.chixing.mapper.StoreMapper;
import com.chixing.pojo.*;
import com.chixing.service.CarService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private StoreCarMapper storeCarMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private MyOrderMapper myOrderMapper;
    @Override
    public ServerResponse getAll() {
        return ServerResponse.getSuccess(carMapper.selectByExample(null));
    }

    @Override
    public ServerResponse getCarDetail(Integer carId) {
        Car car = carMapper.selectByPrimaryKey(carId);
        if (car!=null)
            return ServerResponse.getSuccess(car);
        return ServerResponse.getFail(null);
    }

    // 多条件动态查询
    @Override
    public ServerResponse getCarByContain(String carType,
                                          String carBrand,
                                          Float minCarDailyPrice,
                                          Float maxCarDailyPrice,
                                          String carShiftType,
                                          Integer carSeatNumber,
                                          String orderBy,
                                          Integer pageNum) {
        CarExample carExample = new CarExample();
        if(orderBy.equals("-1"))
            orderBy = null;
        if(carType.equals("-1"))
            carType = null;
        if(carBrand.equals("-1"))
            carBrand = null;

        if(minCarDailyPrice == -1)
            minCarDailyPrice = null;

        if(maxCarDailyPrice == -1)
            maxCarDailyPrice = null;

        if(carShiftType.equals("-1"))
            carShiftType = null;

        if(carSeatNumber == -1)
            carSeatNumber = null;


        if(orderBy != null)
            carExample.setOrderByClause(orderBy);
        CarExample.Criteria criteria = carExample.createCriteria();
        if(carType != null)
            criteria.andCarTypeEqualTo(carType);
        if(carBrand != null)
            criteria.andCarBrandEqualTo(carBrand);
        if(minCarDailyPrice != null)
            criteria.andCarDailyPriceGreaterThanOrEqualTo(minCarDailyPrice);
        if(maxCarDailyPrice != null)
            criteria.andCarDailyPriceLessThanOrEqualTo(maxCarDailyPrice);
        if(carShiftType != null)
            criteria.andCarShiftTypeEqualTo(carShiftType);
        if(carSeatNumber != null)
            criteria.andCarSeatNumberEqualTo(carSeatNumber);
        PageHelper.startPage(pageNum,3);
        List<Car> carList = carMapper.selectByExample(carExample);
        PageInfo<Car> carPageInfo = new PageInfo<>(carList);
        if(carList.size() > 0 )
            return ServerResponse.getSuccess(carPageInfo);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getHotCarToIndex() {
        StoreCarExample storeCarExample = new StoreCarExample();
        storeCarExample.createCriteria().andStatusEqualTo(0);
        storeCarExample.setOrderByClause("car_sale_count desc");
        Page<StoreCar> page = PageHelper.startPage(1,4);
        List<StoreCar> storeCarList = storeCarMapper.selectByExample(storeCarExample);
        PageInfo pageInfo = page.toPageInfo();
        List<Car> carList  = new ArrayList<>();
        for(StoreCar storeCar: storeCarList){
            carList.add(carMapper.selectByPrimaryKey(storeCar.getCarId()));
        }
        pageInfo.setList(carList);
        if(carList.size() > 0)
            return ServerResponse.getSuccess(pageInfo);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getStoreByCarIdAndStoreCity(Integer carId, String storeArea) {
        StoreCarExample storeCarExample = new StoreCarExample();
        storeCarExample.createCriteria().andCarIdEqualTo(carId).andStatusEqualTo(0);
        List<StoreCar> storeCarList = storeCarMapper.selectByExample(storeCarExample);
        List<Store> storeList = new ArrayList<>();
        for(StoreCar storeCar : storeCarList){
            StoreExample storeExample = new StoreExample();
            storeExample.createCriteria().andStoreIdEqualTo(storeCar.getStoreId()).andStoreAreaEqualTo(storeArea);
            List<Store> storeList1 = storeMapper.selectByExample(storeExample);
            if(storeList1.size() > 0 )
                storeList.add(storeList1.get(0));
        }
        if(storeList.size()>0) {
            System.out.println("hello");
            return ServerResponse.getSuccess(storeList);
        }
        return ServerResponse.getFail(null);
    }


    public ServerResponse getStoreByCarIdAndStoreArea(Integer carId,String storeArea){
        StoreCarExample storeCarExample = new StoreCarExample();
        storeCarExample.createCriteria().andCarIdEqualTo(carId);
        List<StoreCar> storeCarList = storeCarMapper.selectByExample(storeCarExample);
        List<Store> storeList = new ArrayList<>();
        for(StoreCar storeCar : storeCarList){
            StoreExample storeExample = new StoreExample();
            StoreExample.Criteria storeExampleCriteria = storeExample.createCriteria();
            storeExampleCriteria.andStoreIdEqualTo(storeCar.getStoreId());
            if(!storeArea.equals("-1")) {
                storeExampleCriteria.andStoreAreaEqualTo(storeArea);
            }
            List<Store> storeList1 = storeMapper.selectByExample(storeExample);
            if(storeList1.size()>0)
                storeList.add(storeList1.get(0));
        }
        if(storeList.size()>0)
            return ServerResponse.getSuccess(storeList);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getCarByContainAndDate(Integer storeId, Date getCarTime, String carType, String carBrand, Float minCarDailyPrice, Float maxCarDailyPrice, String carShiftType, Integer carSeatNumber, String orderBy, Integer pageNum) {
        if(pageNum == null)
            pageNum = 1;
        if(orderBy == null || orderBy.equals("-1"))
            orderBy = null;
        if(carType == null || carType.equals("-1"))
            carType = null;
        if(carBrand == null || carBrand.equals("-1"))
            carBrand = null;

        if(minCarDailyPrice == null || minCarDailyPrice == -1)
            minCarDailyPrice = null;

        if(maxCarDailyPrice == null || maxCarDailyPrice == -1)
            maxCarDailyPrice = null;

        if(carShiftType == null || carShiftType.equals("-1"))
            carShiftType = null;

        if(carSeatNumber == null || carSeatNumber == -1)
            carSeatNumber = null;
        PageHelper.startPage(pageNum,3);
        List<Car> carList = carMapper.selectByContainAndStoreAntReturnCarTime(storeId,getCarTime,carType,carBrand,minCarDailyPrice,maxCarDailyPrice,carShiftType,carSeatNumber,orderBy);
        PageInfo<Car> carPageInfo = new PageInfo<>(carList);
        if(carList.size()>0)
            return ServerResponse.getSuccess(carPageInfo);
        return ServerResponse.getFail(null);
    }

    @Override
    public boolean carByContainIsOk(Car car,String carType,
                                    String carBrand,
                                    Float minCarDailyPrice,
                                    Float maxCarDailyPrice,
                                    String carShiftType,
                                    Integer carSeatNumber) {
        boolean flag = true;
        if(carType != null && !carType.equals("-1"))
            flag = flag && car.getCarType().equals(carType);
        if(carBrand != null && !carBrand.equals("-1"))
            flag = flag && car.getCarBrand().equals(carBrand);
        if(minCarDailyPrice != null && minCarDailyPrice != -1)
            flag = flag && car.getCarDailyPrice() >= minCarDailyPrice;
        if(maxCarDailyPrice != null && maxCarDailyPrice != -1)
            flag = flag && car.getCarDailyPrice() <= maxCarDailyPrice;
        if(carShiftType != null && !carShiftType.equals("-1"))
            flag = flag && car.getCarShiftType().equals(carShiftType);
        if(carSeatNumber != null && carSeatNumber != -1)
            flag = flag && car.getCarSeatNumber().equals(carSeatNumber);

        return flag;
    }

    @Override
    public Car getById(Integer carId) {
        Car car = carMapper.selectByPrimaryKey(carId);
        return car;
    }
}
